﻿namespace devMobile.Controls.Charts
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Shapes;

    public partial class ColumnChartControl : UserControl
    {
        private Dictionary<string, int> _itemsSource;
        public Dictionary<string, int> ItemsSource
        {
            get
            {
                return _itemsSource;
            }
            set
            {
                _itemsSource = value;
                Dispatcher.BeginInvoke(this.Draw);
            }
        }

        public ColumnChartControl()
        {
            InitializeComponent();
        }

        void Draw()
        {
            MainGrid.ShowGridLines = true;
            var rowDefinition = new RowDefinition { Height = new GridLength(this.MainGrid.ActualHeight * .10) };
            MainGrid.RowDefinitions.Add(rowDefinition);
            var rowDefinition2 = new RowDefinition { Height = new GridLength(this.MainGrid.ActualHeight * .80) };
            MainGrid.RowDefinitions.Add(rowDefinition2);
            var rowDefinition3 = new RowDefinition { Height = new GridLength(this.MainGrid.ActualHeight * .10) };
            MainGrid.RowDefinitions.Add(rowDefinition3);

            var lenghtPerRow = MainGrid.ActualWidth / ItemsSource.Count;
            
            var i = default(int);
            var max = this.ItemsSource.Max(item => item.Value);
            foreach (var item in this.ItemsSource)
            {
                var columnDefinition = new ColumnDefinition
                    { Width = new GridLength(lenghtPerRow, GridUnitType.Pixel), MaxWidth = 480 };
                MainGrid.ColumnDefinitions.Add(columnDefinition);

                var textBlockValue = new TextBlock { Text = item.Value.ToString() };
                textBlockValue.SetValue(Grid.ColumnProperty, i + 1);
                textBlockValue.SetValue(Grid.RowProperty, 0);
                textBlockValue.TextAlignment = TextAlignment.Center;
                textBlockValue.FontSize = 16;
                textBlockValue.FontWeight = FontWeights.Bold;

                var rectangle = new Rectangle
                    { Margin = new Thickness(10, 10, 10, 10), Fill = new SolidColorBrush(Colors.Blue) };
                rectangle.SetValue(Grid.ColumnProperty, i + 1);
                rectangle.SetValue(Grid.RowProperty, 1);
                rectangle.Height = (item.Value * MainGrid.ActualHeight * .80) / max;
                rectangle.VerticalAlignment = VerticalAlignment.Bottom;

                var textBlockKey = new TextBlock { Text = item.Key };
                textBlockKey.SetValue(Grid.ColumnProperty, i + 1);
                textBlockKey.SetValue(Grid.RowProperty, 2);
                textBlockKey.TextAlignment = TextAlignment.Center;
                textBlockKey.FontSize = 12;
                textBlockKey.FontWeight = FontWeights.Bold;
                textBlockKey.TextWrapping = TextWrapping.Wrap;


                MainGrid.Children.Add(textBlockValue);
                MainGrid.Children.Add(rectangle);
                MainGrid.Children.Add(textBlockKey);
                i++;
            }

        }
    }
}
